package com.controller;

import com.entity.*;
import com.group.PurOrderModifyGroup;
import com.service.GoodsService;
import com.service.PurNoteService;
import com.service.PurOrderService;
import com.util.CurrentUser;
import com.vo.PurOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/purnote")
public class PurNoteController {
    @Autowired
    private PurNoteService purNoteService;
    @Autowired
    private CurrentUser currentUser;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/getNoteList/{pageIndex}/{pageSize}")
    public Result getNoteList(
          @PathVariable("pageIndex") Integer pageIndex,
          @PathVariable("pageSize") Integer pageSize,
          @RequestBody PurOrderVO purOrderVO
    ){
        PageBean<PurOrder> pageBean = purNoteService.getNoteList(pageIndex, pageSize, purOrderVO);
        return Result.ok(pageBean);
    }

    //取消订单
    @RequestMapping("/removeNoteByOid/{oid}")
    public Result removeNoteByOid(@PathVariable("oid")int oid){
        purNoteService.removeNoteByOid(oid);
        return Result.ok();
    }

    //填写入库信息点击确定按钮的同时，商品的入库状态由未入库改为待入库
    @RequestMapping("/modifyNotestatusByoid/{oid}")
    public Result modifyNotetatusByoid(@PathVariable("oid")int oid){
        purNoteService.updateNotestatusByoid(oid);
        return Result.ok();
    }
    @RequestMapping("/modifyStoragedByOid/{oid}")
    public Result modifyStoragedByOid(@PathVariable("oid")int oid){
        purNoteService.modifyStoragedByOid(oid);
        return Result.ok();
    }

    @PostMapping("/addUnloadGoods")
    public Result addUnloadGoods(@RequestBody UnloadGoods unloadGoods){
        log.info("申请入库，添加入库信息");
        unloadGoods.setEid(currentUser.getCurrentEid());//对接人
        List<GoodsEntity> goodsEntities = goodsService.findAll();
        for (GoodsEntity goodsEntity : goodsEntities) {
            if(goodsEntity.getGname().equals(unloadGoods.getGname())){
                unloadGoods.setGoodsUniqueNo(goodsEntity.getGoodsUniqueNo());
                unloadGoods.setBeforeNumber(goodsEntity.getSum());
                unloadGoods.setGid(goodsEntity.getGid());
            }
        }
        unloadGoods.setGno(32);
        unloadGoods.setIsApprove("待审核");
        unloadGoods.setIsunload("未入库");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String format = sdf.format(new Date());
        unloadGoods.setCreationDate(format);
        log.info("添加部分信息后={}",unloadGoods);
        purNoteService.addUnloadGoods(unloadGoods);
        return new Result<>(200,"添加成功",null);
    }

}
